<?php
function curl($post, $cookie = "", $haveReferer = 1, $Querystring = "")
{
        $POSTFIELDS = "";
        $post = preg_replace("/^(POST|GET)\s+http\:\/\/[^\/]*\/+/m", "\\1 /", $post);

        if (substr($post, 0, 7) == "http://") {
            $filename = $post;
            $post = urltopoststr($post, $cookie, $haveReferer);
        } else {
            preg_match_all("/^Host\:(\s*.*)$/Uimx", $post, $nn);
            $host = trim(chop($nn[1][0]));
            $site = "http://" . $host . "";
            preg_match_all("/^(POST|GET)\s+(.*)\s+HTTP/Umx", $post, $uu);
            $url = preg_replace("/\/+$/", "", $site) . "/" . preg_replace("/^\/+/", "", chop($uu[2][0]));
            preg_match_all("/^Referer\:(\s*.*)$/Uimx", $post, $nn);
            $Referer = trim(chop($nn[1][0]));
            preg_match_all("/(?:\r\n\r\n|\n\n)(.*)$/Uis", $post, $nn);
            $Querystring = ($Querystring) ? $Querystring : trim(chop($nn[1][0]));
            preg_match_all("/boundary=(.*)$/Uim", $post, $nn);
            $boundary = trim(chop($nn[1][0]));
            $filename = $url;
        }
        $user_agent = "Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)";
        $POSTFIELDS = $Querystring;
        if (preg_match_all("/^Cookie\:\s*(.*)$/Uimx", $post, $nn)) {
            $cookie = trim($nn[1][0]);
        }

        $ch = curl_init();

        $timeout = 60;
        curl_setopt($ch, CURLOPT_URL, $filename);
        //curl_setopt ($ch, CURLOPT_MUTE, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)");
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
        curl_setopt($ch, CURLOPT_HEADER, 1); //Ҫ�����ͷ��Ϣ
        if ($uu[1][0] == "POST") curl_setopt($ch, CURLOPT_POST, 1);
        if (preg_match("/multipart/Uisx", $post)) {
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary"));
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        }
        if ($Referer != "") curl_setopt($ch, CURLOPT_REFERER, $Referer);
        if ($cookie != "") curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        if ($Querystring != "" && $uu[1][0] == "POST") {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $Querystring);
        }
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $result = curl_exec($ch);
        curl_close($ch);

        preg_match("/(^.*)(?:\r\n\r\n|\n\n)(.*?)$/Uis", $result, $nn);
        $header = $nn[1];
        $body = $nn[2];

        if (@gzinflate(substr($body, 10))) {
            $body = @gzinflate(substr($body, 10));
        }


        //---------------��chunk����

        if (strpos($result, 'chunk')) {
            $tmpfile = tempnam('d:/temp', 'webcache');
            $fp = fopen($tmpfile, 'w');
            fwrite($fp, $result);
            fclose($fp);
            ob_start();
            readgzfile($tmpfile);
            $result = ob_get_contents();
            ob_end_clean();
            unlink($tmpfile);
        }

        $result = $header . "\r\n\r\n" . $body;
        $nr = $result;
        //$nr=preg_replace("/<(\/?script[^>]*)>/Uisx","<!--\\1-->",$nr);
        return $nr;
}

function curlbase($post,$cookie=""){
    static $cookie;

	    if (substr($post, 0, 7) == "http://") {
			$host=whost($post);
            $Referer="http://{$host}/";
            $filename = $post;
            $post = urltopoststr($post, $cookie, $haveReferer);
        } else {
            preg_match_all("/^Host\:(\s*.*)$/Uimx", $post, $nn);
            $host = trim(chop($nn[1][0]));
            $site = "http://" . $host . "";
            preg_match_all("/^(POST|GET)\s+(.*)\s+HTTP/Umx", $post, $uu);
            $url = preg_replace("/\/+$/", "", $site) . "/" . preg_replace("/^\/+/", "", chop($uu[2][0]));
            preg_match_all("/^Referer\:(\s*.*)$/Uimx", $post, $nn);
            $Referer = trim(chop($nn[1][0]));
            preg_match_all("/(?:\r\n\r\n|\n\n)(.*)$/Uis", $post, $nn);
            $Querystring = ($Querystring) ? $Querystring : trim(chop($nn[1][0]));
            preg_match_all("/boundary=(.*)$/Uim", $post, $nn);
            $boundary = trim(chop($nn[1][0]));
            $filename = $url;
        }

	$timeout=15;
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $filename);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
	curl_setopt($ch, CURLOPT_HEADER, 1);
	//curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	/*if(preg_match("/multipart/Uisx",$post)){
		curl_setopt($ch, CURLOPT_VERBOSE, 1);
		curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary"));
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	}*/
	if($Referer!="") curl_setopt($ch, CURLOPT_REFERER, $Referer);
	if($cookie!="") curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	if($Querystring!=""&&$uu[1][0]=="POST"){
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $Querystring);
    }

	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$result = curl_exec($ch);
	curl_close($ch);
    preg_match("/set\-cookie\:\s*([^\s]*)/",$result,$nn);$cookie=$nn[1];
	return $result;
}



function curl38($url, $cookie="",$Referer="")  
{  
	$timeout = 15;  
	$url=trim(chop($url));  
	$url = str_replace("%", "%25", $url);  
	$ch = curl_init();  
	curl_setopt($ch, CURLOPT_URL, trim(chop($url)));  
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0");  
	if($Referer) curl_setopt($ch, CURLOPT_REFERER, $Referer);  
	curl_setopt($ch, CURLOPT_HEADER, 1);  
	if($cookie){
		curl_setopt($ch, CURLOPT_COOKIE, $cookie);  
	}
	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		//'Content-Type: application/json; charset=utf-8',  
		//'X-Requested-With: XMLHttpRequest',  
		'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',  
		'Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',  
		//'Accept-Encoding: gzip, deflate', 
		'Upgrade-Insecure-Requests: 1',
		'Cache-Control: max-age=0',
		'Connection: Keep-Alive',  
	));  
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
	ob_start();
	curl_setopt($ch, CURLINFO_HEADER_OUT, true);
	$result = curl_exec($ch);
	$info=curl_getinfo($ch, CURLINFO_HEADER_OUT);
	curl_close($ch);  
	$result = ob_get_contents();  
	ob_end_clean();
	echo $result;
	print_r($info);
	exit;
	preg_match("/(^.*)(?:\r\n\r\n|\n\n)(.*?)$/Uis", $result, $nn);  
	$result = $nn[2];  
	if (@gzinflate(substr($result, 10))) {  
		$result = gzinflate(substr($result, 10));  
	}  
	return $result;  
}  







$poststr=<<<TEXT
GET /index.php?XDEBUG_SESSION_START=20170323101137&app=seller_order HTTP/1.1
Host: nn.ilaoyou.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* / *;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://nn.ilaoyou.com/index.php?XDEBUG_SESSION_START=20170323101137&app=seller_order
Cookie: 7d64_sname=nn; CNZZDATA3845711=cnzz_eid%3D1105023350-1482802100-%26ntime%3D1490229872; UM_distinctid=15ad10c9b60a-091e52e1a6e366-44504130-1aeaa0-15ad10c9b612f1; ECM_ID=86ebf84f590347ade8f5bed3bd50d6bf3b1bc760; XDEBUG_SESSION=20170323101338; SSCSum=5; userkey=
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0

TEXT;

//$nr=curlbase($poststr);
//echo $nr;

$url="http://nn.ilaoyou.com/index.php?XDEBUG_SESSION_START=20170323101137&app=seller_order";
//$cookie="7d64_sname=nn; CNZZDATA3845711=cnzz_eid%3D1105023350-1482802100-%26ntime%3D1490229872; UM_distinctid=15ad10c9b60a-091e52e1a6e366-44504130-1aeaa0-15ad10c9b612f1;ECM_ID=86ebf84f590347ade8f5bed3bd50d6bf3b1bc760; XDEBUG_SESSION=20170323101338; SSCSum=5; userkey=";
$cookie="7d64_sname=nn; CNZZDATA3845711=cnzz_eid%3D1105023350-1482802100-%26ntime%3D1490229872; UM_distinctid=15ad10c9b60a-091e52e1a6e366-44504130-1aeaa0-15ad10c9b612f1; ECM_ID=86ebf84f590347ade8f5bed3bd50d6bf3b1bc760; XDEBUG_SESSION=20170323101338; SSCSum=5; userkey=";

$Referer="http://nn.ilaoyou.com/index.php?XDEBUG_SESSION_START=20170323101137&app=seller_order";
$nr=curl38($url,$cookie,$Referer);
echo $nr;

?>